package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController("userAdressBookController")
@RequestMapping("/user/addressBook")
@Api(tags = "C端地址簿接口")
@Slf4j
public class AdressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("新增地址")
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook) {
        log.info("新增地址:{}",addressBook);
        addressBookService.add(addressBook);
        // 修改: 返回插入后的地址簿对象，确保前端能获取到插入的数据
        return Result.success();
    }

    @ApiOperation("查询地址列表")
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        return Result.success(addressBookService.list());
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody Map<String,  Integer> id) {
        addressBookService.setDefault(id.get("id"));
        return Result.success();
    }
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        return Result.success(addressBookService.getDefault(BaseContext.getCurrentId()));
    }
    @ApiOperation("查询指定地址")
    @GetMapping("/{id}")
    public Result<AddressBook> get(@PathVariable Long id) {
        return Result.success(addressBookService.getById(id));
    }
    @ApiOperation("修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("修改地址:{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }
    @ApiOperation("删除地址")
    @DeleteMapping
    public Result delete(Long id) {
        log.info("删除地址:{}",id);
        addressBookService.delete(id);
        return Result.success();
    }
}